package com.example.hackotontest.client;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.example.hackotontest.R;

public class CardAdapter extends BaseAdapter {

	// use the default gallery background image
	int defaultItemBackground;
	// gallery context
	private Context galleryContext;
	// array to store bitmaps to display
	private Bitmap[] imageBitmaps;
	// placeholder bitmap for empty spaces in gallery
	Bitmap placeholder;
	int[] cards = {R.drawable.card0,R.drawable.card1,R.drawable.card2,R.drawable.card3,
			R.drawable.card4,R.drawable.card5,R.drawable.card6,R.drawable.card7,
			R.drawable.card8,R.drawable.card9,R.drawable.card10,R.drawable.card11};
	
	public CardAdapter(Context c) {
		// instantiate context
		galleryContext = c;
		// create bitmap array
		imageBitmaps = new Bitmap[12];
		for (int i = 0; i < imageBitmaps.length; i++) {
			placeholder = BitmapFactory.decodeResource(c.getResources(),cards[i]);
			imageBitmaps[i] = placeholder;
		}
		
		// get the styling attributes - use default Andorid system resources
		TypedArray styleAttrs = galleryContext
				.obtainStyledAttributes(R.styleable.CardGallery);
		
		// get the background resource
		defaultItemBackground = styleAttrs.getResourceId(
				R.styleable.CardGallery_android_galleryItemBackground, 0);
//		
		// recycle attributes
		styleAttrs.recycle();
	}

	@Override
	public int getCount() {
		return imageBitmaps.length;
	}

	@Override
	public Object getItem(int arg0) {
		 return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		 return arg0;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parent) {
		WindowManager wm = (WindowManager) galleryContext.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		int width = display.getWidth();  // deprecated
		int height = display.getHeight();  // deprecated

		
		 //create the view
	    ImageView imageView = new ImageView(galleryContext);
	    //specify the bitmap at this position in the array
	    imageView.setImageBitmap(imageBitmaps[pos]);
	    //set layout options
	    imageView.setLayoutParams(new Gallery.LayoutParams(300, 350));
	    //scale type within view area
	    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
	    //set default gallery item background
	    imageView.setBackgroundResource(defaultItemBackground);
	    //return the view
	    return imageView;
	}
	
	public Bitmap getPic(int posn)
	{
	    return imageBitmaps[posn];
	}
}
